package com.lancoo.znbkxx.teacher.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatDialog;

import com.lancoo.znbkxx.teacher.R;
import com.lancoo.znbkxx.teacher.ui.Presenter;
import com.lancoo.znbkxx.teacher.ui.activity.PrivacyActivity;
import com.lancoo.znbkxx.teacher.utils.DensityUtils;

import static com.lancoo.znbkxx.teacher.utils.DensityUtils.getScreenHeight;
import static com.lancoo.znbkxx.teacher.utils.DensityUtils.getScreenWidth;
//import static com.scwang.smartrefresh.layout.util.DensityUtil.dp2px;

public class PrivacyDialog extends AppCompatDialog {

    private TextView tvCancel, tvConfirm, tvContent;
    private Presenter presenter;
    private Activity mActivity;
    private boolean IsHintCancle = false;

    public PrivacyDialog(Context context, Activity activity) {
        super(context);
        mActivity = activity;
    }

    public PrivacyDialog(Context context, Activity activity, boolean isHintCancle) {
        super(context);
        mActivity = activity;
        IsHintCancle = isHintCancle;
    }

    @Override
    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        setWidthAndHeight();
        getWindow().setWindowAnimations(android.R.style.Animation_Dialog);
        setContentView(R.layout.bk_dialog_privacy);
        if (IsHintCancle) {
            setCanceledOnTouchOutside(true);
        }
        initView();
    }

    private void setWidthAndHeight() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = getScreenWidth(mActivity) - DensityUtils.dp2px(mActivity, 30f);
        lp.height = getScreenHeight(mActivity) - DensityUtils.dp2px(mActivity, 360f);
        getWindow().setAttributes(lp);     //设置生效
    }

    private void initView() {
        tvCancel = findViewById(R.id.cancel_tv);
        if (IsHintCancle) {
            tvCancel.setVisibility(View.GONE);
        }
        tvConfirm = findViewById(R.id.confirm_tv);
        tvContent = findViewById(R.id.content_tv);
        setServicePolicy();
        tvConfirm.setOnClickListener(v -> presenter.onClick(v));
        tvCancel.setOnClickListener(v -> presenter.onCancel(v));
    }

    @Override
    public void show() {
        super.show();
    }

    public PrivacyDialog setPresenter(Presenter presenter) {
        this.presenter = presenter;
        return this;
    }

    private void setServicePolicy() {
        String itemStr = tvContent.getText().toString();
        String serviceStr = "《AI考试辅导用户服务协议》";
        String policyStr = "《AI考试辅导用户隐私政策》";
        int indexService = itemStr.indexOf(serviceStr);
        int indexPolicy = itemStr.indexOf(policyStr);
        SpannableString spannableString = new SpannableString(itemStr);
        //Service Terms 颜色和点击事件
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, PrivacyActivity.class);
                intent.putExtra("isPrivacy", false);
                mActivity.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(true);
            }
        }, indexService, indexService + serviceStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //Privacy Policy 颜色和点击事件
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, PrivacyActivity.class);
                intent.putExtra("isPrivacy", true);
                mActivity.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(true);
            }
        }, indexPolicy, indexPolicy + policyStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(spannableString);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
